package com.zadtek.shopmall.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsListFragment;
import com.zadtek.shopmall.bean.RedItem;
import com.zadtek.shopmall.bean.RedListBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.utils.ToastUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.zadtek.shopmall.activities.RedEnvelopActivity.TAB_1;
import static com.zadtek.shopmall.activities.RedEnvelopActivity.TAB_2;
import static com.zadtek.shopmall.activities.RedEnvelopActivity.TAB_3;

/**
 * Created by Zackv on 2018/4/11.
 */

public class RedEnvelopFragment extends AbsListFragment {

    private int currentTabIndex = TAB_1;

    private RedEnvelopAdapter redEnvelopAdapter;
    private List<RedItem> data = new LinkedList<>();
    private RedListBean bean;
    private int page = 1;
    private int pagesize = 10;

    public RedEnvelopFragment(){

    }

    public static RedEnvelopFragment Instance(int tabIndex){
        RedEnvelopFragment instance = new RedEnvelopFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("tabIndex",tabIndex);
        instance.setArguments(bundle);
        return instance;
    }
    @Override
    protected void firstRequestData() {
        Bundle bundle = getArguments();
        if(bundle!=null){
            currentTabIndex = bundle.getInt("tabIndex");
        }
        switch (currentTabIndex){
            case TAB_1:
                loadDataTab1();
                break;
            case TAB_2:
                loadDataTab2();
                break;
            case TAB_3:
                loadDataTab3();
                break;
        }
    }

    private void loadDataTab3() {
        Map<String, Object> param = NetTool.newParams();
        param.put("page",page);
        param.put("page",pagesize);
        param.put("type",3);
        NetTool.getApi().redenvelop(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<RedListBean>>() {
                    @Override
                    public void onData(BaseResponse<RedListBean> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            if(value.getData().getBonus().size() != 0){
                                data.addAll(value.getData().getBonus());
                                redEnvelopAdapter.loadMoreComplete();
                                redEnvelopAdapter.notifyDataSetChanged();
                                page++;
                            }else{
                                redEnvelopAdapter.loadMoreEnd();
                            }
                        }
                    }
                });

    }

    private void loadDataTab2() {
        Map<String, Object> param = NetTool.newParams();
        param.put("page",page);
        param.put("page",pagesize);
        param.put("type",2);
        NetTool.getApi().redenvelop(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<RedListBean>>() {
                    @Override
                    public void onData(BaseResponse<RedListBean> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            if(value.getData().getBonus().size() != 0){
                                data.addAll(value.getData().getBonus());
                                redEnvelopAdapter.loadMoreComplete();
                                redEnvelopAdapter.notifyDataSetChanged();
                                page++;
                            }else{
                                redEnvelopAdapter.loadMoreEnd();
                            }
                        }
                    }
                });
    }

    private void loadDataTab1() {
        Map<String, Object> param = NetTool.newParams();
        param.put("page",page);
        param.put("page",pagesize);
        param.put("type",1);
        NetTool.getApi().redenvelop(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<RedListBean>>() {
                    @Override
                    public void onData(BaseResponse<RedListBean> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            if(value.getData().getBonus().size() != 0){
                                data.addAll(value.getData().getBonus());
                                redEnvelopAdapter.loadMoreComplete();
                                redEnvelopAdapter.notifyDataSetChanged();
                                page++;
                            }else{
                                redEnvelopAdapter.loadMoreEnd();
                            }
                        }
                    }
                });
    }

    @Override
    protected void loadMore() {
        switch (currentTabIndex){
            case TAB_1:
                loadDataTab1();
                break;
            case TAB_2:
                loadDataTab2();
                break;
            case TAB_3:
                loadDataTab3();
                break;
        }
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        redEnvelopAdapter = new RedEnvelopAdapter(R.layout.red_envelop_item,data);
        return redEnvelopAdapter;
    }

    @Override
    protected void OnItemClick(int position) {
        ToastUtil.showToast("click+"+position);
    }

    @Override
    protected void OnRefresh() {
        setRefreshing(false);
    }

    private class RedEnvelopAdapter extends BaseQuickAdapter<RedItem,BaseViewHolder>{

        public RedEnvelopAdapter(int layoutResId, @Nullable List<RedItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, RedItem item) {
            helper.setText(R.id.tv_red_money,"￥"+String.valueOf(item.getMoney()));
            helper.setText(R.id.tv_red_name,item.getTitle());
            helper.setText(R.id.tv_red_reason,item.getReason());
            helper.setText(R.id.tv_red_validity,item.getTimeString());
            helper.setVisible(R.id.checkbox,false);
        }
    }
}
